﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Common;
using Job.Logic.BaseContent;
using System.Collections;

namespace Job.Web.userControls.Admin
{
    public partial class BaseContent : BaseCtrl
    {
        private static Hashtable SYSTEM_INIT_PARAMS = null;

        private BaseInfo GetConfig(int type)
        {
            if (null == SYSTEM_INIT_PARAMS)
            {
                SYSTEM_INIT_PARAMS = new Hashtable();
                List<BaseInfo> list = GetInitStructs();
                foreach (BaseInfo info in list)
                {
                    SYSTEM_INIT_PARAMS.Add(
                        Convert.ToString((int)info.SubjectId), info);
                }
            }

            string key = Convert.ToString(type);
            if (null == SYSTEM_INIT_PARAMS[key])
            {
                BaseInfo info = new BaseInfo();
                info.MaxDescSize = 100;
                info.SubjectId = type;
                info.TitleReadOnly = false;
                return info;
            }
            else
            {
                return (BaseInfo)SYSTEM_INIT_PARAMS[key];
            }
        }

        private int controlType
        {
            get { return myR["controlType"].IntValue; }
        }

        private int contentId
        {
            get { return myR["contentId"].IntValue; }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnableControl();
                LoadData();
            }
        }

        private void EnableControl()
        {
            BaseInfo baseInfo = GetConfig(controlType);

            if (baseInfo.TitleReadOnly)
            {
                txtTitle.ReadOnly = true;
                chkStatusId.Checked = true;
                chkStatusId.Enabled = false;
                txtOrderNo.Value = "0";
                txtOrderNo.Disabled = true;
                TRResouce.Visible = false;
            }
            else
            {
                txtTitle.ReadOnly = false;
                chkStatusId.Checked = true;
                chkStatusId.Enabled = true;
                txtOrderNo.Disabled = false;
                TRResouce.Visible = true;  
            }
            if (baseInfo.MaxDescSize <= 0)
            {
                //TRDescription.Style.Add("display", "none");
                TRDescription.Visible = false;
                rfvDesc.Enabled = false;
                rfvDesc.EnableClientScript = false;
                if (baseInfo.MaxDescSize < 0)
                {
                    TRResouce.Visible = false;
                }
            }
            else
            {
                //TRDescription.Style.Add("display", "");
                TRDescription.Visible = true;
                txtDescription.MaxLength = baseInfo.MaxDescSize;
                rfvDesc.Enabled = true;
                rfvDesc.EnableClientScript = true;                
            }
        }


        private void LoadData()
        {
            if (contentId <= 0 && controlType > 0)
            {
                //新增
                hidContentId.Value = "";
                return;
            }
            
            BaseContentLogic blogic = new BaseContentLogic();
            JobBaseContent baseContent = null;
            if ( contentId > 0 ) 
            {
                baseContent = blogic.GetContentByContentId(contentId);
            }
            else
            {
                if (controlType > 0 ) 
                    baseContent = blogic.GetContentBySubjectId(controlType);
            }


            if ( baseContent == null ) 
            {
                //查询不到错误提示
                string message = Server.UrlEncode(string.Format("指定的ID({0})不存在，请与管理员联系!按返回按钮将导航到管理首页", contentId));
                string urlPath = Server.UrlEncode("~/AdminWeb/index.htm");
                string url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url);
            }
            else 
            {
                hidContentId.Value = baseContent.ContentId.ToString();
                txtTitle.Text = baseContent.Title;
                txtDescription.Text = baseContent.Description;
                if (baseContent.Content != null)
                    FCKeditor1.Value = Server.HtmlDecode(Functions.ByteArrayToString(baseContent.Content));
                if (null != baseContent.Count && null != baseContent.Count.Value) 
                    txtCount.Text = baseContent.Count.Value.ToString();
                chkStatusId.Checked = Convert.ToBoolean(baseContent.StatusId);
                lbllastUpdatetxt.Text = baseContent.LastUpdateUser + "　(" + baseContent.LastUpdateTime.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
                txtOrderNo.Value = baseContent.OrderNo.ToString();
                if ( baseContent.Resource != null ) txtResource.Text = baseContent.Resource;
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            CommonLogic logic = new CommonLogic();
            BaseContentLogic blogic = new BaseContentLogic();
            JobBaseContent baseContent = null;
            string contentId = hidContentId.Value.Trim();
            if (!contentId.Equals(""))
            {
                baseContent = blogic.GetContentByContentId(Convert.ToInt32( contentId));
            }
            else
            {
                baseContent = new JobBaseContent(); 
            }
            baseContent.Content = Functions.StringToByteArray(Server.HtmlEncode(FCKeditor1.Value));

            baseContent.Description = txtDescription.Text.Trim();
            baseContent.LastUpdateTime = logic.GetServerTime();
            MySession session = new MySession();
            baseContent.LastUpdateUser =
                (null == session) ? " " : session.GetAdminUserFromSession().Name;
            baseContent.StatusId = Convert.ToInt32(chkStatusId.Checked);
            baseContent.SubjectId = Constants.GetListIdByInfoId(controlType);
            baseContent.Title = txtTitle.Text.Trim();
            baseContent.Resource = txtResource.Text.Trim();
            int orderNo = 0;
            Int32.TryParse(txtOrderNo.Value.Trim(), out orderNo);
            baseContent.OrderNo = orderNo;

            try
            {
                //string contentId = hidContentId.Value.Trim();
                if (!contentId.Equals(""))
                {
                    //baseContent.ContentId = Convert.ToInt32(contentId);
                    blogic.Update(baseContent);
                }
                else
                {
                    blogic.Insert(baseContent);
                }

                int listId = Constants.GetListIdByInfoId(controlType);
                //if (listId != controlType)
                //{
                //    string url = "AdminMain.aspx?controlType=" + Convert.ToString(listId);
                //    Response.Redirect(url);
                //}
                string message = message = Server.UrlEncode("保存成功，请按返回按钮查看信息!");
                string urlPath = string.Empty;
                string url = string.Empty;

                if (listId != controlType)
                {
                    urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString(listId));
                    url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
                }
                else
                {
                    urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString(listId) + "&ContentID=" + baseContent.ContentId);
                    url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
                }
                Response.Redirect(url, false);
            }
            catch (Exception ex)
            {
                string message = Server.UrlEncode(String.Format("保存失败，请与管理员联系！按返回按钮重新输入。错误信息: {0}!",ex.Message));
                string urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString(controlType));
                string url = string.Format("~/ErrorMessage.aspx?flag=0&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url);
            }
        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}";
            int listType = Constants.GetListIdByInfoId(controlType);
            Response.Redirect(string.Format(url, listType));
        }

        private List<BaseInfo> GetInitStructs()
        {
            List<BaseInfo> results = new List<BaseInfo>();

            BaseInfo item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyNews;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyDescription;
            item.TitleReadOnly = true;
            item.MaxDescSize = 40;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyFastInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyHRInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyIndustryInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyManagerInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyBBS;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.CompanyContactMethod;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.HunterDescription;
            item.TitleReadOnly = true;
            item.MaxDescSize = 60;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.HunterNewsInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.HunterManagerAreaInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.HunterContactMethod;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.ServiceDescription;
            item.TitleReadOnly = true;
            item.MaxDescSize = 60;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.ServiceSuggestion;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.ServiceQuestionInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.ServiceAnalysisInfo;
            item.TitleReadOnly = false;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.ServiceContactMethod;
            item.TitleReadOnly = true;
            item.MaxDescSize = 0;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.OtherFriendLink;
            item.TitleReadOnly = true;
            item.MaxDescSize = -1;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.OtherCompanyHelp;
            item.TitleReadOnly = true;
            item.MaxDescSize = -1;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.OtherPersonHelp;
            item.TitleReadOnly = true;
            item.MaxDescSize = -1;
            results.Add(item);

            item = new BaseInfo();
            item.SubjectId = (int)Constants.SubjectType.OtherCustomerCenter;
            item.TitleReadOnly = true;
            item.MaxDescSize = -1;
            results.Add(item);

            return results;
        }

    }
}